const App = getApp();
const utils = require("../../../../utils/util.js");
import * as echarts from '../../../../ec-canvas/echarts';
let chartLine
function getOption(title,clue_num) {
  var option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      top: '0%',
      left: '2%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'value',
      boundaryGap: [0, 0.01]
    },
    yAxis: {
      type: 'category',
      axisLabel: {
        interval: 0,
        //rotate: 40,
         formatter: function(value) {
          var res = value;
          if(res.length > 4) {
            res = res.substring(0, 3) + "..";
          }
          return res;
       }
      },
      data: title
    },
    series: [
      {
        name: '2011年',
        type: 'bar',
        itemStyle: {
          normal: {
            color: '#fd561f'
          }
        },
        data: clue_num
      }
    ]

  };
  return option;
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    show_pop: false,
    role_id: '',
    currentIndex: 0,
    top_data: {},
    core_data: {},
    warning_state: {},
    clue_data: {},
    building_rank: {},
    turnover_state: {},
    promotion_state:{},
    tabs: [
      { index: 0, name: '日报', type: 'today' },
      { index: 1, name: '本周', type: 'this_week' },
      { index: 2, name: '本月', type: 'this_month' },
    ],
    ecLine: {
      onInit: function (canvas, width, height, dpr) {
        //初始化echarts元素，绑定到全局变量，方便更改数据
        chartLine = echarts.init(canvas, null, {
          width: width,
          height: height,
          devicePixelRatio: dpr
        });
        canvas.setChart(chartLine);
        //可以先不setOption，等数据加载好后赋值，
        //不过那样没setOption前，echats元素是一片空白，体验不好，所有我先set。
        return chartLine
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    setTimeout(() => {
      this.getTeamData()
    }, 500);
    this.setData({
      role_id: wx.getStorageSync('role_id')
    })
    console.log(wx.getStorageSync('role_id'))
  },

  handleShowPop() {
    this.setData({
      show_pop: true
    })
  },
  handleHidePop() {
    this.setData({
      show_pop: false
    })
  },
  handledetail() {
    wx.navigateTo({
      url: '../../franchisee/invalid/tel-list',
    })
  },
  /**
   * tab切换
   */
  handleTabChange(e) {
    this.setData({
      currentIndex: e.currentTarget.dataset.index
    })
    this.getTeamData()
  },

  //获取团队数据
  getTeamData() {
    let _this = this
    App._post_form('Operation/getOperationHomeData', {
      time_type: _this.data.tabs[_this.data.currentIndex].type
    }, function (result) {
      _this.setData({
        top_data: result.data.top_data,
        core_data: result.data.core_data,
        warning_state: result.data.warning_state,
        clue_data: result.data.clue_data,
        building_rank: result.data.building_rank,
        turnover_state: result.data.turnover_state,
        promotion_state: result.data.promotion_state
      })
      let { title,clue_num } = result.data.building_rank
      var option = getOption(title,clue_num);
      // console.log(chartLine)
      chartLine.setOption(option);
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  onPullDownRefresh:function(){
    this.getTeamData()
  },
  onReady: function () {
    //页面渲染完成 第三运行
    //console.log('onReady');
  },


})